// import 'dart:convert';
// import 'dart:io';

// import 'package:flutter/cupertino.dart';
// import 'package:get/get.dart';
// import 'package:lib_basemodel/google/protobuf/any.pb.dart';
// import 'package:lib_basemodel/lib_basemodel.dart';
// import 'package:lib_basemodel/server_client/common/car.pb.dart';
// import 'package:lib_basemodel/server_client/message.pb.dart';
// import 'package:lib_basemodel/server_client/ride.pb.dart';
// import 'package:lib_basemodel/server_client/rtc.pb.dart';
// import 'package:lib_basemodel/server_client/tacit.pb.dart';
// import 'package:lib_basemodel/server_client/user.pb.dart';
// import 'package:lib_baseview/lib_baseview.dart';
// import 'package:lib_rtc/lib_rtc.dart';
// import 'package:lib_runtime/lib_runtime.dart';
// import 'package:lib_user/lib_user.dart';
// import 'package:m_chatroom/m_chatroom.dart';
// import 'package:m_chatroom/src/template/mic_list/mic_seat_item_widget.dart';
// import 'package:m_chatroom/src/template/music/model/room_music_center_controller.dart';
// import 'package:m_main/m_main.dart';
// import 'package:permission_handler/permission_handler.dart';
// import 'package:protobuf/protobuf.dart' as $pb;
// import 'package:flutter_cache_manager/flutter_cache_manager.dart';

// import '../manager/privilege_dialog.dart';
// import '../manager/violation_close_dialog.dart';

// /// 处理房间刷新消息，麦位、设置、排麦、热度等
// class RoomCmdMsgHandler with SocketMsgHandler {
//   final RoomPageModel roomPageModel;

//   RoomCmdMsgHandler(this.roomPageModel);

//   @override
//   Set<Scene> get sceneList => Scene.values.toSet();

//   Channel get channel => roomPageModel.roomModel.channel!;

//   @override
//   handleMsg(Scene scene, Message msg) async {
//     if (scene == Scene.MIC_CHANGE) {
//       RoomMicChange micChangeMessage =
//           msg.data.content.unpackInto(RoomMicChange());

//       channel.broadcaster.clear();
//       channel.broadcaster.addAll(micChangeMessage.broadcaster);

//       channel.roomTypeObj.layout = micChangeMessage.micLayout;
//       channel.roomTypeObj.layoutName = micChangeMessage.layoutName;

//       channel.showRoomOwner = micChangeMessage.showRoomOwner;
//       RootProxyModel.instance?.removeUnUseMicKey();

//       if (roomPageModel.roomModel.isOnMicSeat) {
//         var permissionStatus = await PermissionUtil.checkPermissions(
//             Get.context!, Permission.microphone);
//         if (permissionStatus == PermissionStatus.granted) {
//           await roomPageModel.roomModel.roomRtc?.setBroadcaster();
//           if (roomPageModel.isCurrUserMicOn) {
//             roomPageModel.roomModel.roomRtc?.openMic();
//           } else {
//             roomPageModel.roomModel.roomRtc?.closeMic();
//           }
//         }
//       } else {
//         roomPageModel.roomModel.roomRtc?.setAudience();
//         // 下麦后停止播放
//         Get.delete<RoomMusicCenterController>(force: true);
//       }
//       roomPageModel.roomModel.startTimerIfNeed();
//       roomPageModel.roomModel.onMic = roomPageModel.roomModel.isOnMicSeat;
//     } else if (scene == Scene.USER_JOIN) {
//       // 用户进入房间
//       RoomUserJoin userJoin = msg.data.content.unpackInto(RoomUserJoin());
//       roomPageModel.userJoinRoomAddItem(userJoin);

//       // 播放座驾入场动效
//       String carAnimationUrl = userJoin.user.car.carMetaInfo.animationLink;
//       if (carAnimationUrl.isNotEmpty) {
//         DisplayGiftConfig config = DisplayGiftConfig(
//             carAnimationUrl, userJoin.user.car.carMetaInfo.leftImgLink,
//             showAnimation: false, key: GlobalKey());
//         eventCenter.emit(Event(EventConstant.addNewGiftConfig, data: config));
//       }
//     } else if (scene == Scene.SEND_GIFT) {
//       RoomSendGift roomSendGift = msg.data.content.unpackInto(RoomSendGift());
//       List<int> uidList =
//           roomSendGift.receivers.map((e) => e.id.toInt()).toList();
//       DisplayGiftConfig config = DisplayGiftConfig(
//           roomSendGift.item.dynamicResource, roomSendGift.item.icon,
//           key: GlobalKey(),
//           uid: roomSendGift.send.id.toInt(),
//           count: roomSendGift.itemCount,
//           uidList: uidList,
//           giftId: roomSendGift.item.id.toInt(),
//           showAnimation: true);
//       eventCenter.emit(Event(EventConstant.addNewGiftConfig, data: config));
//     } else if (scene == Scene.REAL_TIME_RANK_CHANGE) {
//       //  实时打赏榜变更 消息
//       RoomRealTimeRankChange roomRealTimeRankChange =
//           msg.data.content.unpackInto(RoomRealTimeRankChange());

//       channel.giftRank.clear();
//       channel.giftRank.addAll(roomRealTimeRankChange.rank);
//     } else if (scene == Scene.GIFT_VALUE_CHANGE) {
//       // 打赏相关数值变更 RoomGiftValueChange

//       RoomGiftValueChange roomGiftValueChange =
//           msg.data.content.unpackInto(RoomGiftValueChange());

//       channel.hotValue = roomGiftValueChange.hotValue;
//     } else if (scene == Scene.MIC_QUEUE_CHANGE) {
//       //  排麦列表变更 RoomMicQueueChange
//       RoomMicQueueChange roomMicQueueChange =
//           msg.data.content.unpackInto(RoomMicQueueChange());

//       channel.waitQueue.clear();
//       channel.waitQueue.addAll(roomMicQueueChange.waitQueue);
//       channel.carOwnerMicQueue.clear();
//       channel.carOwnerMicQueue.addAll(roomMicQueueChange.carOwnerMicQueue);
//     } else if (scene == Scene.VIP_PARK_CHANGE) {
//       // 贵宾车位变更 RoomVipParkChange');

//       RoomVipParkChange roomVipParkChange =
//           msg.data.content.unpackInto(RoomVipParkChange());

//       channel.vipPark.clear();
//       channel.vipPark.addAll(roomVipParkChange.vipPark);
//     } else if (scene == Scene.ROOM_META_DATA_CHANGE) {
//       // 房间基础信息变更
//       RoomMetaDataChange roomMetaDataChange =
//           msg.data.content.unpackInto(RoomMetaDataChange());

//       channel.title = roomMetaDataChange.title;
//       channel.theme = roomMetaDataChange.theme;
//       channel.notice = roomMetaDataChange.notice;

//       channel.isPrivacy = roomMetaDataChange.isPrivacy;

//       channel.freePub = roomMetaDataChange.freePub;

//       /// 是否自由上麦
//       channel.freePublicChat = roomMetaDataChange.freePublicChat;

//       /// 是否关闭公屏

//       // channel.roomTypeObj.layout = roomMetaDataChange.micLayout;
//       // channel.roomTypeObj.layoutName = roomMetaDataChange.layoutName;

//       channel.backgroundUrl = roomMetaDataChange.backgroundUrl;
//       channel.giftValueCountSwitch = roomMetaDataChange.giftValueCountSwitch;
//       channel.status = roomMetaDataChange.status.name;

//       channel.roomTypeObj.type = roomMetaDataChange.type;
//       channel.hasPassword = roomMetaDataChange.hasPassword;

//       channel.roomLabel = roomMetaDataChange.label;
//       channel.roomLabelDesc = roomMetaDataChange.labelDesc;

//       // 更新公屏状态，打开、关闭公屏
//       roomPageModel.barrageListModel.enable = channel.freePublicChat;
//     } else if (scene == Scene.ROLE_CHANGE) {
//       /// '角色变更 RoomRoleChange'

//       RoomRoleChange roomRoleChange =
//           msg.data.content.unpackInto(RoomRoleChange());

//       if (UserManager.instance.currentUser?.id.toInt() ==
//           roomRoleChange.userId.toInt()) {
//         channel.role = roomRoleChange.role;
//       }
//       if (channel.owner.id.toInt() == roomRoleChange.userId.toInt()) {
//         channel.owner.role = roomRoleChange.role;
//       }
//       channel.broadcaster.forEach((element) {
//         if (element.user.id.toInt() == roomRoleChange.userId.toInt()) {
//           element.user.role = roomRoleChange.role;
//         }
//       });
//       channel.userList.forEach((element) {
//         if (element.id.toInt() == roomRoleChange.userId.toInt()) {
//           element.role = roomRoleChange.role;
//         }
//       });
//       channel.waitQueue.forEach((element) {
//         if (element.user.id.toInt() == roomRoleChange.userId.toInt()) {
//           element.user.role = roomRoleChange.role;
//         }
//       });
//       channel.carOwnerMicQueue.forEach(
//         (element) {
//           if (element.user.id.toInt() == roomRoleChange.userId.toInt()) {
//             element.user.role = roomRoleChange.role;
//           }
//         },
//       );
//     } else if (scene == Scene.MANAGER_OPERATION) {
//       // 用户被踢出房间
//       RoomManage roomManage = msg.data.content.unpackInto(RoomManage());
//       if (roomManage.operation == ManagerOperation.KICK_OUT &&
//           roomManage.userId.toInt() ==
//               UserManager.instance.currentUser?.id.toInt()) {
//         await roomPageModel.disposePageAndData();
//         showToast(roomManage.text.isNotEmpty
//             ? roomManage.text
//             : T.current!.kickout_by_admin);
//         return;
//       } else if (roomManage.operation ==
//           ManagerOperation.VIOLATION_CLOSE_ROOM) {
//         await roomPageModel.disposePageAndData();
//         ViolationCloseDialog.show(roomManage.text);
//         return;
//       } else if (roomManage.operation == ManagerOperation.CLOSE_ROOM) {
//         await roomPageModel.disposePageAndData();
//         if (roomManage.text.isNotEmpty) {
//           showToast(roomManage.text);
//         }
//         return;
//       }
//     } else if (scene == Scene.PROGRAM_CHANGE) {
//       /// 房间内活动变更
//       /// 此参数为可选参数，这里需要判断有没有实例,如果没有，需要先创建
//       if (!channel.hasProgram()) {
//         channel.program = ProgramInfo();
//       }
//       RoomProgramChange roomProgramChange =
//           msg.data.content.unpackInto(RoomProgramChange());

//       channel.program.startTime = roomProgramChange.startTime;
//       channel.program.startTimeSeconds = roomProgramChange.startTimeSeconds;
//       channel.program.topic = roomProgramChange.topic;
//       channel.program.programId = roomProgramChange.programId;
//       channel.program.desc = roomProgramChange.desc;
//       channel.program.date = roomProgramChange.date;
//     } else if (scene == Scene.FLYING_SCREEN) {
//       if (!(GlobalConfig.timeEnterMain == 0 ||
//           msg.time.toInt() < GlobalConfig.timeEnterMain)) {
//         RoomFlyingScreen roomFlyingScreen =
//             msg.data.content.unpackInto(RoomFlyingScreen());

//         // MainPageModel.instance.addGlobalAnimation(roomFlyingScreen);
//         eventCenter
//             .emit(Event(EventConstant.floatScreen, data: roomFlyingScreen));
//       }
//     }

//     /// 兜风房消息
//     else if (scene == Scene.TACIT_INVITE) {
//       /// 默契测试邀请消息
//       TacitPush tacitInvite = msg.data.content.unpackInto(TacitPush());
//       roomPageModel.acceptInvitation(tacitInvite);
//       RLog.d("TacitPush_TACIT_INVITE：  ${tacitInvite.toProto3Json()}");
//     } else if (scene == Scene.TACIT_ABORT) {
//       /// 默契问答 拒绝
//       TacitPush tacitAbort = msg.data.content.unpackInto(TacitPush());
//       roomPageModel.rejectionTacitAnswer(tacitAbort);
//       RLog.d("TacitPush_TACIT_ABORT：  ${tacitAbort.toProto3Json()}");
//     } else if (scene == Scene.TACIT_QUEST) {
//       /// 默契问答 提问
//       TacitPush tacitQuest = msg.data.content.unpackInto(TacitPush());
//       roomPageModel.questionTacitAnswer(tacitQuest);

//       RLog.d("TacitPush_TACIT_QUEST：  ${tacitQuest.toProto3Json()}");
//     } else if (scene == Scene.TACIT_END) {
//       /// 结束
//       TacitPush tacitEnd = msg.data.content.unpackInto(TacitPush());
//       roomPageModel.tacitEnd(tacitEnd);

//       RLog.d("TacitPush_TACIT_END：  ${tacitEnd.toProto3Json()}");
//       // RLog.d("TacitPush_TACIT_END：  ${jsonDecode(msg.data.content)}");
//     } else if (scene == Scene.RIDE_ROOM_PUSH) {
//       RidePush ridePush = msg.data.content.unpackInto(RidePush());
//       roomPageModel.roomModel.ridePush = ridePush;

//       var isBoss = roomPageModel.roomModel.meWithSlot?.micType ==
//           MicTypeEnum.CAR_OWNER_MIC;

//       if (ridePush.hasEnd()) {
//         await roomPageModel.disposePageAndData();
//         PrivilegeDialog.show(ridePush.end.subReason,
//             title: ridePush.end.reason);
//         return;
//       }

//       // 嘉宾弹窗提示
//       if (!isBoss) {
//         if (ridePush.privilege.operation == PrivilegeOperation.USE) {
//           PrivilegeDialog.show(ridePush.privilege.content.desc);
//         } else if (ridePush.privilege.operation == PrivilegeOperation.END) {
//           PrivilegeDialog.show(T.current!.df_privilege_ending);
//         }
//       }
//     } else if (scene == Scene.OFF_MIC_CHANGE) {
//       RoomOffMicChange offMicChange =
//           msg.data.content.unpackInto(RoomOffMicChange());
//       channel.offMicUser.clear();
//       channel.offMicUser.addAll(offMicChange.offMicUser);
//     } else if (scene == Scene.ROOM_MIC_SHOW_IMG) {
//       RoomMicShowImg micImg = msg.data.content.unpackInto(RoomMicShowImg());
//       eventCenter.emit(Event(MicSeatItemWidget.msgShowMicImage, data: micImg));

//       ///麦上表情发送一次公屏消息
//       if (micImg.type == 3) {
//         if (micImg.uid.toInt() == UserManager.instance.currentUid) {
//           roomPageModel.sendMicImgMsg(micImg.offMicImgUrl);
//         }
//       }
//     } else if (scene == Scene.ROOM_EFFECT) {
//       var roomEffect = msg.data.content.unpackInto(RoomEffect());
//       var localFiles = <String>[];
//       for (var _ in roomEffect.imgList) {
//         var file = await DefaultCacheManager().getSingleFile(_);
//         localFiles.add(await ImageFileUtil.clipCircle(file));
//       }

//       DisplayGiftConfig config = DisplayGiftConfig(roomEffect.url, '',
//           key: GlobalKey(),
//           imgList: localFiles,
//           textList: roomEffect.textList,
//           showAnimation: false);
//       eventCenter.emit(Event(EventConstant.addNewGiftConfig, data: config));
//     } else {
//       RLog.d('Unhandled msg: ${msg.data.scene}');
//     }
//     roomPageModel.roomModel.notifyListeners();

//     // TODO PageModel通过RootModel来刷新，需测试是否有不刷新的异常
//     // roomModel.notifyListeners();
//   }
// }
